package com.sinocst.sap2;
//import com.sap.conn.jco.ext.DataProviderException;
import com.sap.conn.jco.ext.DestinationDataEventListener;
import com.sap.conn.jco.ext.DestinationDataProvider;
import com.sap.tc.logging.ExceptionManager;

import java.util.HashMap;
import java.util.Properties;

/**
 * @Project: JavaWebSAPAPI
 * @Author: zhang10092009@hotmail.com
 * @Date: 2018/2/24  16:52
 * @Description:
 */
public class JCOProvider implements DestinationDataProvider {
    private HashMap<String, Properties> secureDBStorage = new HashMap<String, Properties>();
    private DestinationDataEventListener eL;
    @Override
    public Properties getDestinationProperties(String destinationName) {
        try
        {
            //read the destination from DB
            Properties p = secureDBStorage.get(destinationName);

            if(p!=null)
            {
                //check if all is correct, for example
                if(p.isEmpty())
                    //throw new DataProviderException(DataProviderException.Reason.INVALID_CONFIGURATION, "destination configuration is incorrect", null);
                    //throw new Exception("destination configuration is incorrect");
                    return p;
            }
        } catch(RuntimeException re) {
            new Exception(re.getMessage());
//            throw new DataProviderException(DataProviderException.Reason.INTERNAL_ERROR, re);
        }
        return null;
    }

    @Override
    public boolean supportsEvents() {
        return true;
    }

    @Override
    public void setDestinationDataEventListener(DestinationDataEventListener destinationDataEventListener) {
        this.eL = destinationDataEventListener;
    }

    public void changePropertiesForABAP_AS(String destName, Properties properties) {
        synchronized(secureDBStorage) {
            if(properties==null) {
                if(secureDBStorage.remove(destName)!=null)
                    eL.deleted(destName);
            } else {
                secureDBStorage.put(destName, properties);
                eL.updated(destName); // create or updated
            }
        }
    }
}
